package com.javasoft.service.ZSW;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javasoft.dao.ZSW.DataDictionaryDao;
import com.javasoft.entity.ZSW.DataDictionaryEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("dataDictionaryService")
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryDao, DataDictionaryEntity>implements DataDictionaryService {

    @Override
    public String update(DataDictionaryEntity dataDictionaryEntity) {
        if (dataDictionaryEntity.getId()!= null) {
            boolean updateResult = updateById(dataDictionaryEntity);
            if (updateResult) {
                return "更新操作成功";
            } else {
                return "更新操作失败";
            }
        } else {
            // 新增去重逻辑，先查询数据库中是否已存在相同 fixedtype 和 fixedtypenumber 的记录
            List<DataDictionaryEntity> existingEntities = list(new QueryWrapper<DataDictionaryEntity>()
                    .eq("fixedtype", dataDictionaryEntity.getFixedtype())
                    .eq("fixedtypenumber", dataDictionaryEntity.getFixedtypenumber()));
            if (!existingEntities.isEmpty()) {
                // 如果存在，返回提示信息字符串
                return "数据重复，无法新增，fixedtype: " + dataDictionaryEntity.getFixedtype() + ", fixedtypenumber: " + dataDictionaryEntity.getFixedtypenumber();
            }
            boolean saveResult = save(dataDictionaryEntity);
            if (saveResult) {
                return "新增操作成功";
            } else {
                return "新增操作失败";
            }
        }
    }//新增&更改

    @Override
    public boolean delete(List<Integer> ids){
        return removeByIds(ids);
    }//删除


    @Override
    public Page<DataDictionaryEntity> page(Integer current,Integer size,String type){
        LambdaQueryWrapper<DataDictionaryEntity>wrapper = new LambdaQueryWrapper<>();
        if (!"".equals(type)){
            wrapper.like(DataDictionaryEntity::getFixedtype,type);
        }
        Page<DataDictionaryEntity> page =page(new Page<>(
                current,
                size
        ),
                wrapper);
        return page;
    }//查询


    @Override
    public Integer add(DataDictionaryEntity dataDictionaryEntity) {
        return baseMapper.insert(dataDictionaryEntity);
    }//新增老师版


    @Override
    public IPage<DataDictionaryEntity> page(DataDictionaryEntity dataDictionaryEntity, int page, int pageline) {
        LambdaQueryWrapper<DataDictionaryEntity> query = Wrappers.lambdaQuery();
        query.eq(DataDictionaryEntity::getFixedtype,dataDictionaryEntity.getFixedtype());

        //分页参数
        if(page==0){
            page=1;
        }
        if(pageline==0){
            pageline=10;
        }
       IPage<DataDictionaryEntity> ipage = Page.of(page,pageline);
        return baseMapper.selectPage(ipage, query);
    }//分页老师版
}
